package cn.edu.hstc.wm.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import cn.edu.hstc.wm.R;
import cn.edu.hstc.wm.ui.base.BaseFragment;
import cn.edu.hstc.wm.utils.LogUtil;

/**
 * 网络没有打开时显示的页面,用在Activity中
 * 
 * @author CX
 * @since 2014-01-01
 */
public class NetworkDisableFragment extends BaseFragment implements OnClickListener {
	/**
	 * 一个回调接口,使用这个Fragment的Activity要实现它,网络没有打开时需要调用
	 * 
	 */
	public interface CallBack {
		/**
		 * 网络设置
		 * 
		 * @param context
		 *            设置网络的上下文
		 */
		public void setNetWork(Context context);
	}

	private static final String TAG = NetworkDisableFragment.class.getName();

	private static NetworkDisableFragment sFragment = null;

	private static CallBack mCallbacks;

	/** 设置按钮 */
	private TextView mSetupView;

	/**
	 * 获取该Fragment实例的方法
	 * 
	 * @param bundle
	 * @return
	 */
	public static NetworkDisableFragment getInstance(Bundle bundle) {
		if (sFragment == null) {
			sFragment = new NetworkDisableFragment();
			sFragment.setArguments(bundle);
		}
		return sFragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		LogUtil.i(TAG, "onCreateView");
		View rootView = inflater.inflate(R.layout.network_disable_layout, container, false);
		mSetupView = (TextView) rootView.findViewById(R.id.tv_network_disable_setup);
		mSetupView.setOnClickListener(this);
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		LogUtil.i(TAG, "onActivityCreated");
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		LogUtil.i(TAG, "onAttach");
		super.onAttach(activity);
		if (!(activity instanceof CallBack)) {
			throw new IllegalStateException("Activity must implement fragment's callbacks.");
		}
		mCallbacks = (CallBack) activity;
	}

	@Override
	public void onDetach() {
		LogUtil.i(TAG, "onDetach");
		super.onDetach();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_network_disable_setup:
			// 回调
			mCallbacks.setNetWork(getActivity());
			break;

		default:
			break;
		}
	}

}
